package com.lifp.annotation.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Description 切面类
 * @Author lfp
 * @Date 2022/8/24
 * @Version 1.0
 */
@Component
@Aspect
@Slf4j
public class AspectPermission {

    //切点，标注了自定义的注解@ApiPermission的方法作为切点
    @Pointcut("@annotation(com.lifp.annotation.ApiPermission)")
    public void point(){}

    @Before("point()")
    public void before(){
        log.info("Before-》 通过token解析用户信息，并查询用户权限");
    }

    @After("point()")
    public void after(){
        log.info("After-》 记录日志");
    }

    @AfterReturning("point()")
    public void afterReturning(){
        log.info("AfterReturning-》 记录日志");
    }

    @AfterThrowing("point()")
    public void afterThrowing(){
        log.info("AfterThrowing-》 记录日志");
    }

    @SneakyThrows
    @Around("point()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        log.info("Around-》 记录日志");

        ObjectMapper mapper = new ObjectMapper();

        Class<?> aClass = proceedingJoinPoint.getTarget().getClass();
        String className = aClass.getName();

        Object[] args = proceedingJoinPoint.getArgs();
        String kind = proceedingJoinPoint.getKind();

        //当前方法
        Signature signature = proceedingJoinPoint.getSignature();
        //类对象
        Class declaringType = signature.getDeclaringType();
        //类名
        String declaringTypeName = signature.getDeclaringTypeName();
        //该方法是否为public
        int modifiers = signature.getModifiers();
        //方法名
        String signatureName = signature.getName();

        log.info("类名：{}，args：{},kind：{}," +
                "declaringType：{},declaringTypeName：{},modifiers：{}" +
                ",signatureName：{}",
                className,mapper.writeValueAsString(args),kind,
                declaringType,declaringTypeName,modifiers,signatureName);

        Object proceed = proceedingJoinPoint.proceed();
        return proceed;
    }
}
